package com.aidong.web.controller;

/**
 * Created by john on 16-11-3.
 */

import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.SysUserDao;
import com.aidong.model.LoginUser;
import com.aidong.model.RequestInfo;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.SysUser;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController extends BaseController {
    @Resource
    SysUserDao sysUserDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;

    public LoginController() {
    }

    @RequestMapping({"login"})
    public ModelAndView login(HttpServletRequest request) {
        String url = (String) request.getAttribute("backUrl");
        if (this.chkNull(url)) {
            url = request.getContextPath() + "/";
        }
        return (new ModelAndView("login")).addObject("backUrl", url);
    }

    @RequestMapping({"_login"})
    @ResponseBody
    public String _login(@RequestParam(value = "loginName", required = false) String loginName, @RequestParam(value = "password", required = false) String password, @RequestParam(value = "shopCode", required = false) String shopCode, @RequestParam(value = "code", required = false) String code, HttpServletRequest request) {
        if (!this.chkNull(loginName) && !this.chkNull(password)) {
            SysUser user = sysUserDao.chkLogin(loginName, this.getEncodePwd(loginName, password));
            if (user == null) {
                return this.getResult(false, "用户名或密码错误!");
            } else if (user.getStatus() != null && user.getStatus().intValue() == 1) {
                if (user.getExpire() != null && user.getExpire().intValue() == 0 && (user.getExpireDate() == null || user.getExpireDate().getTime() >= System.currentTimeMillis())) {
                    RequestInfo req = this.userSession.getRequestInfo();
                    if (req != null && !this.chkNull(user.getSecuityLevel()) && ("1".compareTo(user.getSecuityLevel()) >= 0 || req.chkAllowIp(user.getAllowIp()))) {
                        DicShopCode shop = null;
                        if (!this.chkNull(shopCode)) {
                            shop = (DicShopCode) this.dicShopCodeDao.getById(shopCode);
                            if (shop == null || this.chkNullZ(shop.getStatus())) {
                                return this.getFail(-1, "登录的门店错误");
                            }

                            if (user.getLoginAllShop().intValue() != 1 && !this.sysUserDao.chkUserShop(user.getUserCode(), shop.getCode())) {
                                return this.getFail(-2, "不能登录到该门店");
                            }
                        } else {
                            shop = this.sysUserDao.getUserDefalutShop(user.getUserCode());
                        }

                        if (shop == null) {
                            return this.getResult(false, "没有可登录的门店!");
                        } else {
                            request.getSession().removeAttribute("menuAuth");
                            this.userSession.addLoginUser(user, shop, request);
                            this.sysUserDao.updateLogin(user.getUserCode(), req.getIpAddr(), req.getOsType());
                            this.addUserLog("登录", "操作系统:" + req.getOsType() + ",浏览器信息:" + req.getUserAgent());
                            if (user.getPwdTime() != null && user.getPwdTime().getTime() < System.currentTimeMillis() - 5184000000L) {
                                return this.getSuccess("9", "您的密码已经过期,请设置新密码!");
                            } else {
                                return this.getResult(true, "登录成功");
                            }
                        }
                    } else {
                        return this.getResult(false, "限制当前IP登录!");
                    }
                } else {
                    return this.getResult(false, "用户已过期!");
                }
            } else {
                return this.getResult(false, "用户已停用!");
            }
        } else {
            return this.getResult(false, "用户名或密码错误!");
        }
    }

    @RequestMapping({"_logout"})
    @ResponseBody
    public String _logout(HttpServletRequest request) {
        LoginUser user = this.userSession.getLoginUser();
        if (user != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            this.addUserLog("退出登录", "登录时间:" + sdf.format(new Date(user.getLoginTime())));
            this.userSession.removeLoginUser(request);
        }

        return this.getResult(true, "退出成功");
    }
}